<?php
echo '<script type="text/javascript">';
require APP . WEBROOT_DIR . DS . "js" . DS . "validate" . DS . "stat_employee.js";
require APP . WEBROOT_DIR . DS . "js" . DS . "validate" . DS . "expense_add.js";
echo '</script>';
?>
<?php echo $javascript->link('filter_actions'); ?>
<div id="div_imprimir">
    <?php
    echo $this->Html->link(
            $this->Html->image('impresora.png', array('width' => 40, 'alt' => 'imprimir')), array('controller' => 'stats', 'action' => 'imprimir_employee_stats'), array('escape' => false, 'target' => '_blank')
    );
    ?>
</div>
<div class="stats index">
    <h2><?php __('Estadística por Empleado'); ?></h2>
    <a href="" class="filtro_a" onclick="show_filter(); return false;"><?php echo __('Filtro', true); ?></a>
    <input type="hidden" id="input_filtro" value="0">
    <div id="filtro_busqueda" class="filtro" style="display: block">
        <fieldset>
            <?php echo $form->create('Stat', array('url' => array('controller' => 'stats', 'action' => 'employee'))); ?>
            <?php
            echo $this->Form->input('employee_id', array('label' => 'Empleado', 'options' => $employees));
            ?>
            <div class="date_range">
                <label>Rango de Fechas</label>
                <?php
                echo $form->input('date_from', array('label' => false));
                ?>
                <span>-</span>
                <?php
                echo $form->input('date_to', array('label' => false));
                ?>
                <span><?php echo $this->Html->link(__('Hoy', true), array(), array('onclick' => 'fecha_hoy(); return false;')); ?></span>
                &nbsp;&nbsp;&nbsp;
                <span><?php echo $this->Html->link(__('Últimos 7 Días', true), array(), array('onclick' => 'fecha_semana(); return false;')); ?></span>
                <?php
                echo $form->input('hidden_actual_date', array('type' => 'hidden', 'value' => date('d/m/Y')));
                echo $form->input('hidden_week_date', array('type' => 'hidden', 'value' => date('d/m/Y', strtotime('-7 day', strtotime(date('Y-m-d H:i:s'))))));
                ?>
            </div>
            <?php
            echo $form->submit(__('Filtrar', true));
            echo $form->end();
            ?>
        </fieldset>
    </div>
    <?php
    $date_from_format = '';
    $date_to_format = '';
    if (isset($this->data['Stat']['date_from']) && !empty($this->data['Stat']['date_from'])) {
        list($d, $m, $a) = explode("/", $this->data['Stat']['date_from']);
        $date_from_format = $a . "-" . $m . "-" . $d;
    }
    if (isset($this->data['Stat']['date_to']) && !empty($this->data['Stat']['date_to'])) {
        list($d, $m, $a) = explode("/", $this->data['Stat']['date_to']);
        $date_to_format = $a . "-" . $m . "-" . $d;
    }

    $monto_total = 0;
    $total_pagado = 0;
    $total_a_pagar = 0;
    $algo_sin_pagar = 0;
    ?>
    <h3>Cumpleaños</h3>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php echo 'Fecha'; ?></th>
            <th><?php echo 'Dirección'; ?></th>
            <th><?php echo 'Tareas'; ?></th>
            <th><?php echo 'Monto'; ?></th>
            <th><?php echo 'Estado'; ?></th>
            <th class="actions"><?php __('Acciones'); ?></th>
        </tr>
        <?php
        if (empty($birthdays)) {
            ?>
            <tr>
                <td colspan="6">No se registra ningún Cumpleaños</td>
            </tr>
            <?php
        }

        $i = 0;
        $total_birthday = 0;
        $aux_no_pagado = 0;
        foreach ($birthdays as $stat_birthday):
            $class = null;
            if ($i++ % 2 == 0) {
                $class = ' class="altrow"';
            }
            ?>
            <tr<?php echo $class; ?>>
                <td><?php echo $stat_birthday['date']; ?>&nbsp;</td>
                <td><?php echo $stat_birthday['address']; ?>&nbsp;</td>
                <td><?php echo implode(', ', $stat_birthday['details']); ?>&nbsp;</td>
                <td>$<?php echo $stat_birthday['cost'] != NULL ? $stat_birthday['cost'] : 0; ?>&nbsp;</td>
                <?php
                $monto_total += $stat_birthday['cost'] != NULL ? $stat_birthday['cost'] : 0;
                if ($stat_birthday['paid'] == 0) {
                    $estado = 'No Pagado';
                    $class = 'no_paid';
                    $total_a_pagar += $stat_birthday['cost'] != NULL ? $stat_birthday['cost'] : 0;
                    $aux_no_pagado = 1;
                    $algo_sin_pagar = 1;
                } else {
                    $estado = 'Pagado';
                    $class = 'paid';
                    $total_pagado += $stat_birthday['cost'] != NULL ? $stat_birthday['cost'] : 0;
                }
                ?>
                <td class="<?php echo $class; ?>"><?php echo $estado; ?>&nbsp;</td>
                <td class="actions">
                    <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller' => 'jobs', 'action' => 'view', $stat_birthday['job_id']), array('title' => __('Ver Cumpleaños', true), 'escape' => false)); ?>
                    <?php
                    if ($stat_birthday['paid'] == 0) {
                        if ($header_tipo_usuario == GROUP_ADMIN) {
                            //si es admin damos la opción para "marcar como pagado"
                            echo $this->Html->link($this->Html->image('icons/flag_blue.png'), array('controller' => 'jobs', 'action' => 'pay', $stat_birthday['job_id'], $this->data['Stat']['employee_id']), array('title' => __('Marcar como Pagado', true), 'escape' => false));
                        }
                        echo $this->Html->link($this->Html->image('icons/money_dollar.png'), array('controller' => 'jobs', 'action' => 'pay', $stat_birthday['job_id'], $this->data['Stat']['employee_id'], 1), array('title' => __('Pagar con Caja', true), 'escape' => false));
                    }
                    ?>
                </td>
            </tr>
            <?php
            $total_birthday += $stat_birthday['cost'] != NULL ? $stat_birthday['cost'] : 0;
        endforeach;

        if (!empty($birthdays)) {
            ?>
            <tr class="tr_total">
                <td colspan="3">Total</td>
                <td>$<?php echo $total_birthday ?></td>
                <td>&nbsp</td>
                <td class="actions">
                </td>
            </tr>
            <?php
        }
        ?>
    </table>
    <?php
    if ($aux_no_pagado == 1) {
        ?>
        <div id="div_pagar_todo">
            <?php
            if ($header_tipo_usuario == GROUP_ADMIN) {
                echo $this->Html->link(__('Marcar Todos los Cumpleaños como Pagados', true), array('controller' => 'jobs', 'action' => 'pay_all', 'birthday', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format), array('class' => 'marcar_pagado'));
                echo '&nbsp';
            }
            echo $this->Html->link(__('Pagar Todos los Cumpleaños con Caja', true), array('controller' => 'jobs', 'action' => 'pay_all', 'birthday', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format, 1));
            ?>
        </div>
        <?php
    }
    ?>
    <br />
    <hr />
    <br />
    <h3>Eventos</h3>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php echo 'Fecha'; ?></th>
            <th><?php echo 'Dirección'; ?></th>
            <th><?php echo 'Tareas'; ?></th>
            <th><?php echo 'Monto'; ?></th>
            <th><?php echo 'Estado'; ?></th>
            <th class="actions"><?php __('Acciones'); ?></th>
        </tr>
        <?php
        if (empty($events)) {
            ?>
            <tr>
                <td colspan="6">No se registra ningún Evento</td>
            </tr>
            <?php
        }

        $i = 0;
        $total_event = 0;
        $aux_no_pagado = 0;
        foreach ($events as $stat_event):
            $class = null;
            if ($i++ % 2 == 0) {
                $class = ' class="altrow"';
            }
            ?>
            <tr<?php echo $class; ?>>
                <td><?php echo $stat_event['date']; ?>&nbsp;</td>
                <td><?php echo $stat_event['address']; ?>&nbsp;</td>
                <td><?php echo implode(', ', $stat_event['details']); ?>&nbsp;</td>
                <td>$<?php echo $stat_event['cost'] != NULL ? $stat_event['cost'] : 0; ?>&nbsp;</td>
                <?php
                $monto_total += $stat_event['cost'] != NULL ? $stat_event['cost'] : 0;
                if ($stat_event['paid'] == 0) {
                    $estado = 'No Pagado';
                    $class = 'no_paid';
                    $total_a_pagar += $stat_event['cost'] != NULL ? $stat_event['cost'] : 0;
                    $aux_no_pagado = 1;
                    $algo_sin_pagar = 1;
                } else {
                    $estado = 'Pagado';
                    $class = 'paid';
                    $total_pagado += $stat_event['cost'] != NULL ? $stat_event['cost'] : 0;
                }
                ?>
                <td class="<?php echo $class; ?>"><?php echo $estado; ?>&nbsp;</td>
                <td class="actions">
                    <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller' => 'jobs', 'action' => 'view', $stat_event['job_id']), array('title' => __('Ver Evento', true), 'escape' => false)); ?>
                    <?php
                    if ($stat_event['paid'] == 0) {
                        if ($header_tipo_usuario == GROUP_ADMIN) {
                            //si es admin damos la opción para "marcar como pagado"
                            echo $this->Html->link($this->Html->image('icons/flag_blue.png'), array('controller' => 'jobs', 'action' => 'pay', $stat_event['job_id'], $this->data['Stat']['employee_id']), array('title' => __('Marcar como Pagado', true), 'escape' => false));
                        }
                        echo $this->Html->link($this->Html->image('icons/money_dollar.png'), array('controller' => 'jobs', 'action' => 'pay', $stat_event['job_id'], $this->data['Stat']['employee_id'], 1), array('title' => __('Pagar con Caja', true), 'escape' => false));
                    }
                    ?>
                </td>
            </tr>
            <?php
            $total_event += $stat_event['cost'] != NULL ? $stat_event['cost'] : 0;
        endforeach;

        if (!empty($events)) {
            ?>
            <tr class="tr_total">
                <td colspan="3">Total</td>
                <td>$<?php echo $total_event ?></td>
                <td>&nbsp</td>
                <td class="actions">
                </td>
            </tr>

            <?php
        }
        ?>
    </table>
    <?php
    if ($aux_no_pagado == 1) {
        ?>
        <div id="div_pagar_todo">
            <?php
            if ($header_tipo_usuario == GROUP_ADMIN) {
                echo $this->Html->link(__('Marcar Todos los Eventos como Pagados', true), array('controller' => 'jobs', 'action' => 'pay_all', 'event', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format), array('class' => 'marcar_pagado'));
                echo '&nbsp';
            }
            echo $this->Html->link(__('Pagar Todos los Eventos con Caja', true), array('controller' => 'jobs', 'action' => 'pay_all', 'event', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format, 1));
            ?>
        </div>
        <?php
    }
    ?>
    <br />
    <hr />
    <br />
    <h3>Obras de Teatro</h3>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php echo 'Fecha'; ?></th>
            <th><?php echo 'Dirección'; ?></th>
            <th><?php echo 'Monto'; ?></th>
            <th><?php echo 'Estado'; ?></th>
            <th class="actions"><?php __('Acciones'); ?></th>
        </tr>
        <?php
        if (empty($theaters)) {
            ?>
            <tr>
                <td colspan="5">No se registra ninguna Obra de Teatro</td>
            </tr>
            <?php
        }

        $i = 0;
        $total_theater = 0;
        $aux_no_pagado = 0;
        foreach ($theaters as $stat_theater):
            $class = null;
            if ($i++ % 2 == 0) {
                $class = ' class="altrow"';
            }
            ?>
            <tr<?php echo $class; ?>>
                <td><?php echo $stat_theater['date']; ?>&nbsp;</td>
                <td><?php echo $stat_theater['address']; ?>&nbsp;</td>
                <td>$<?php echo $stat_theater['cost'] != NULL ? $stat_theater['cost'] : 0; ?>&nbsp;</td>
                <?php
                $monto_total += $stat_theater['cost'] != NULL ? $stat_theater['cost'] : 0;
                if ($stat_theater['paid'] == 0) {
                    $estado = 'No Pagado';
                    $class = 'no_paid';
                    $total_a_pagar += $stat_theater['cost'] != NULL ? $stat_theater['cost'] : 0;
                    $aux_no_pagado = 1;
                    $algo_sin_pagar = 1;
                } else {
                    $estado = 'Pagado';
                    $class = 'paid';
                    $total_pagado += $stat_theater['cost'] != NULL ? $stat_theater['cost'] : 0;
                }
                ?>
                <td class="<?php echo $class; ?>"><?php echo $estado; ?>&nbsp;</td>
                <td class="actions">
                    <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller' => 'theaters', 'action' => 'view', $stat_theater['theater_id']), array('title' => __('Ver Obra de Teatro', true), 'escape' => false)); ?>
                    <?php
                    if ($stat_theater['paid'] == 0) {
                        if ($header_tipo_usuario == GROUP_ADMIN) {
                            //si es admin damos la opción para "marcar como pagado"
                            echo $this->Html->link($this->Html->image('icons/flag_blue.png'), array('controller' => 'theaters', 'action' => 'pay', $stat_theater['theater_id'], $this->data['Stat']['employee_id']), array('title' => __('Marcar como Pagado', true), 'escape' => false));
                        }
                        echo $this->Html->link($this->Html->image('icons/money_dollar.png'), array('controller' => 'theaters', 'action' => 'pay', $stat_theater['theater_id'], $this->data['Stat']['employee_id'], 1), array('title' => __('Pagar con Caja', true), 'escape' => false));
                    }
                    ?>
                </td>
            </tr>
            <?php
            $total_theater += $stat_theater['cost'] != NULL ? $stat_theater['cost'] : 0;
        endforeach;

        if (!empty($theaters)) {
            ?>
            <tr class="tr_total">
                <td colspan="2">Total</td>
                <td>$<?php echo $total_theater ?></td>
                <td>&nbsp</td>
                <td class="actions">
                </td>
            </tr>

            <?php
        }
        ?>
    </table>
    <?php
    if ($aux_no_pagado == 1) {
        ?>
        <div id="div_pagar_todo">
            <?php
            if ($header_tipo_usuario == GROUP_ADMIN) {
                echo $this->Html->link(__('Marcar Todos las Obras como Pagadas', true), array('controller' => 'theaters', 'action' => 'pay_all', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format), array('class' => 'marcar_pagado'));
                echo '&nbsp';
            }
            echo $this->Html->link(__('Pagar Todos las Obras con Caja', true), array('controller' => 'theaters', 'action' => 'pay_all', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format, 1));
            ?>
        </div>
        <?php
    }
    ?>
    <br />
    <hr />
    <br />
    <h3>Gastos Extra</h3>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php echo 'Fecha'; ?></th>
            <th><?php echo 'Descripción'; ?></th>
            <th><?php echo 'Monto'; ?></th>
            <th><?php echo 'Estado'; ?></th>
            <th class="actions"><?php __('Acciones'); ?></th>
        </tr>
        <?php
        if (empty($expenses)) {
            ?>
            <tr>
                <td colspan="5">No se registra ningún Gasto Extra</td>
            </tr>
            <?php
        }

        $i = 0;
        $total_expense = 0;
        $aux_no_pagado = 0;
        foreach ($expenses as $stat_expense):
            $class = null;
            if ($i++ % 2 == 0) {
                $class = ' class="altrow"';
            }
            ?>
            <tr<?php echo $class; ?>>
                <td><?php echo $stat_expense['date']; ?>&nbsp;</td>
                <td><?php echo $stat_expense['description']; ?>&nbsp;</td>
                <td>$<?php echo $stat_expense['value'] != NULL ? $stat_expense['value'] : 0; ?>&nbsp;</td>
                <?php
                $monto_total += $stat_expense['value'] != NULL ? $stat_expense['value'] : 0;
                if ($stat_expense['paid'] == 0) {
                    $estado = 'No Pagado';
                    $class = 'no_paid';
                    $total_a_pagar += $stat_expense['value'] != NULL ? $stat_expense['value'] : 0;
                    $aux_no_pagado = 1;
                    $algo_sin_pagar = 1;
                } else {
                    $estado = 'Pagado';
                    $class = 'paid';
                    $total_pagado += $stat_expense['value'] != NULL ? $stat_expense['value'] : 0;
                }
                ?>
                <td class="<?php echo $class; ?>"><?php echo $estado; ?>&nbsp;</td>
                <td class="actions">
                    <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller' => 'expenses', 'action' => 'view', $stat_expense['expense_id']), array('title' => __('Ver Gasto', true), 'escape' => false)); ?>
                    <?php
                    if ($stat_expense['paid'] == 0) {
                        if ($header_tipo_usuario == GROUP_ADMIN) {
                            //si es admin damos la opción para "marcar como pagado"
                            echo $this->Html->link($this->Html->image('icons/flag_blue.png'), array('controller' => 'expenses', 'action' => 'pay', $stat_expense['expense_id']), array('title' => __('Marcar como Pagado', true), 'escape' => false));
                        }
                        echo $this->Html->link($this->Html->image('icons/money_dollar.png'), array('controller' => 'expenses', 'action' => 'pay', $stat_expense['expense_id'], 1), array('title' => __('Pagar con Caja', true), 'escape' => false));
                    }
                    ?>
                </td>
            </tr>
            <?php
            $total_expense += $stat_expense['value'] != NULL ? $stat_expense['value'] : 0;
        endforeach;

        if (!empty($expenses)) {
            ?>
            <tr class="tr_total">
                <td colspan="2">Total</td>
                <td>$<?php echo $total_expense ?></td>
                <td>&nbsp</td>
                <td class="actions">
                </td>
            </tr>

            <?php
        }
        ?>
    </table>
    <?php
    if ($aux_no_pagado == 1) {
        ?>
        <div id="div_pagar_todo">
            <?php
            if ($header_tipo_usuario == GROUP_ADMIN) {
                echo $this->Html->link(__('Marcar Todos los Gastos como Pagados', true), array('controller' => 'expenses', 'action' => 'pay_all', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format), array('class' => 'marcar_pagado'));
                echo '&nbsp';
            }
            echo $this->Html->link(__('Pagar Todos los Gastos con Caja', true), array('controller' => 'expenses', 'action' => 'pay_all', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format, 1));
            ?>
        </div>
        <?php
    }
    ?>
    <br />
    <hr />
    <br />
    <div class="clear"></div>
    <?php
    if ($algo_sin_pagar == 1) {
        ?>
        <div id="div_pagar_todo">
            <hr />
            <hr />
            <hr />
            <br />
            <?php
            if ($header_tipo_usuario == GROUP_ADMIN) {
                echo $this->Html->link(__('Marcar como Pagado Todo que este sin pagar', true), array('controller' => 'stats', 'action' => 'pay_all', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format), array('class' => 'marcar_pagado'));
                echo '&nbsp';
            }
            echo $this->Html->link(__('Pagar con Caja Todo lo que este sin pagar', true), array('controller' => 'stats', 'action' => 'pay_all', $this->data['Stat']['employee_id'], $date_from_format, $date_to_format, 1));
            ?>
            <br />
            <br />
            <hr />
            <hr />
            <hr />
        </div>
        <?php
    }
    ?>
    <br />
    <div class="resume_total">
        <p><b>Monto Total = </b>$<?php echo $monto_total; ?></p>
        <br />
        <p><b>Total Pagado = </b>$<?php echo $total_pagado; ?></p>
        <p><b>Total a Pagar = </b> $<?php echo $total_a_pagar; ?></p>
    </div>
    <br />
    <br />
    <?php echo $this->Form->create('Expense', array('url' => array('controller' => 'expenses', 'action' => 'add'))); ?>
    <fieldset>
        <legend><?php __('Agregar Gasto Extra'); ?></legend>
        <?php
        echo $this->Form->input('expensetype_id', array('label' => 'Tipo de Gasto'));
        echo $this->Form->input('employee_id', array('type' => 'hidden', 'value' => $this->data['Stat']['employee_id']));
        echo $this->Form->input('value', array('label' => 'Monto'));
        echo $this->Form->input('date', array('type' => 'text', 'label' => 'Fecha'));
        echo $this->Form->input('description', array('label' => 'Descripción'));
        ?>
    </fieldset>
    <?php echo $this->Form->end(__('Crear', true)); ?>
</div>
<script type="text/javascript">

    (function($){
        // Date picker
        $("#StatDateFrom").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });
        // Date picker
        $("#StatDateTo").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });
        // Date picker
        $("#ExpenseDate").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

    })(jQuery);

    function fecha_hoy(){
        var hoy = $('#StatHiddenActualDate').val();
        $('#StatDateFrom').val(hoy);
        $('#StatDateTo').val(hoy);
        $('#StatEmployeeForm').submit();
    }

    function fecha_semana(){
        var hoy = $('#StatHiddenActualDate').val();
        var semana = $('#StatHiddenWeekDate').val();
        $('#StatDateFrom').val(semana);
        $('#StatDateTo').val(hoy);
        $('#StatEmployeeForm').submit();
    }
</script>